package com.org.zp.ctrl.myzhaopin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.HttpUtils.CallBack;
import com.org.base.widget.LoadMoreListView;
import com.org.base.widget.LoadMoreListView.OnLoadMoreListener;
import com.org.base.widget.LoadMoreListViewWithProgress;
import com.org.zp.R;
import com.org.zp.adapter.MyzhaopinLayoutAdapter;
import com.org.zp.app.RecApp;
import com.org.zp.ctrl.search.PositionActivity;
import com.org.zp.entity.myzp.LayoutItem;

public class ApplyRecordActivity extends BaseActivity {
	private RecApp mApp;

	private TextView headerTitle;
	private Button backbutton, top_right_btn;

	private List<LayoutItem> positions = new ArrayList<LayoutItem>();

	private LoadMoreListViewWithProgress viewWhitProgress;
	private LoadMoreListView listView;
	private LinearLayout linear;
	private MyzhaopinLayoutAdapter adapter;

	private int currentPage;
	private int totalPage;
	private int perPageCount = 14;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				loadDataTask();
				break;
			case 1:
				adapter.notifyDataSetChanged();
				break;
			case 2:

				Toast toast2 = Toast.makeText(ApplyRecordActivity.this,
						"无反应", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();
				break;

			}
			super.handleMessage(msg);
		}
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.position_search_result);
		mApp = (RecApp) this.getApplication();

		if (!(ActivityTaskManager.getInstance()
				.containsName("ApplyRecordActivity"))) {
			ActivityTaskManager.getInstance().putActivity(
					"ApplyRecordActivity", this);
		}

		// 返回按钮
		backbutton = (Button) findViewById(R.id.back);
		backbutton.setText("  返回");
		backbutton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityTaskManager.getInstance().removeActivity(
						"ApplyRecordActivity");
			}
		});
		// 标题
		headerTitle = (TextView) findViewById(R.id.headTitle);
		headerTitle.setText("职位列表 ");

		top_right_btn = (Button) findViewById(R.id.top_right_button);
		top_right_btn.setVisibility(View.GONE);

		init();

	}

	private void init() {

		linear = (LinearLayout) findViewById(R.id.search_job_result_linear);

		adapter = new MyzhaopinLayoutAdapter(ApplyRecordActivity.this, positions);

		viewWhitProgress = new LoadMoreListViewWithProgress(this);
		listView = viewWhitProgress.getListView();

		listView.setAdapter(adapter);
		listView.setOnLoadMoreListener(new OnLoadMoreListener() {

			@Override
			public void onLoadMore() {
				loadDataTask();
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (((RecApp) getApplicationContext()).isNetWorkConnection) {

					Intent intent = new Intent(ApplyRecordActivity.this,
							PositionActivity.class);
					intent.putExtra("Job_number", positions.get(position)
							.getId());
					startActivity(intent);

				} else {
					Toast toast2 = Toast.makeText(ApplyRecordActivity.this,
							"网络连接断开，请检查网络", Toast.LENGTH_SHORT);
					toast2.setGravity(Gravity.CENTER, 0, 0);
					toast2.show();
				}

			}
		});

		linear.addView(viewWhitProgress);

		Message msg0 = new Message();
		msg0.what = 0;
		handler.sendMessage(msg0);

	}

	private void loadDataTask() {

		currentPage = currentPage + 1;
		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "jobsApplyList");
		map.put("uid", mApp.user.getuID());
		map.put("currentPage", String.valueOf(currentPage));
		map.put("rowCountPerPage", perPageCount + "");
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				Log.e("TMG", "result = " + response.toString());
				try {
					String succeed = response.getString("succeed");
					if (succeed.equals("000")) {

						String currentPage = response.getString("currentPage");
						String totalPage = response.getString("totalPage");

						if (currentPage.equals(totalPage)) {
							listView.loadComplete(false);
						}

						String data = response.getString("dataList");
						JSONArray list = new JSONArray(data);

						for (int i = 0; i < list.length(); i++) {
							LayoutItem info = new LayoutItem();
							JSONObject log = list.getJSONObject(i);
							info.setId(log.getString("jobs_id"));
							info.setTitle(log.getString("jobs_name"));
							info.setCompany(log.getString("company_name"));
							info.setDate(log.getString("apply_addtime"));
							info.setOther(log.getString("personal_look"));

							positions.add(info);
						}

					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

				// We need notify the adapter that the data have been changed
				adapter.notifyDataSetChanged();
				viewWhitProgress.show();

			}

			@Override
			public void onFailure(Throwable e) {
				// TODO Auto-generated method stub

			}
		});

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity(
					"ApplyRecordActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
